<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary"
                     @click="setCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <el-row>
        <tree-table :data="cateList"
                    :columns="columns"
                    :selection-type="false"
                    :expand-type="false"
                    show-index
                    index-text="ID"
                    border
                    class="tree-table">
          <template slot="isVaild"
                    slot-scope="scope">
            <i class="el-icon-success"
               style="color: lightgreen"
               v-if="!scope.row.cat_deleted"></i>
            <i class="el-icon-error"
               style="color: red"
               v-else></i>
          </template>
          <template slot="order"
                    slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success"
                    v-else-if="scope.row.cat_level === 1">二级</el-tag>
            <el-tag type="warning"
                    v-else>三级</el-tag>
          </template>
          <template slot="operation"
                    slot-scope="scope">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-edit"
                       @click="editDialog(scope.row.id)">编辑</el-button>
            <el-button size="mini"
                       type="danger"
                       icon="el-icon-delete">删除</el-button>
          </template>
        </tree-table>
      </el-row>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pagenum"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size="5"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="添加角色"
               :visible.sync="isAddDialogVisible"
               width="50%"
               @colse="addDialogColse">
      <el-form ref="addCateFromRef"
               :model="addCateFrom"
               :rules="addCateFromRules"
               label-width="100px">
        <el-form-item label="分类名称:"
                      prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader v-model="selectdCateId"
                       :options="parentCateList"
                       :props="cascaderProps"
                       @change="parentCateChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="isAddDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created () {
    this.getCateList()
  },
  data () {
    return {
      queryInfo: {
        type: '3',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      cateList: [],
      columns: [
        {
          prop: 'cat_name',
          label: '分类名称'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isVaild'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      isAddDialogVisible: false,
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFromRules: {
        cat_name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }]
      },
      parentCateList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      selectdCateId: []
    }
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('商品分类获取失败')
      }

      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getUserList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getUserList()
    },
    setCateDialog () {
      this.getParentCateList()
      this.isAddDialogVisible = true
    },
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: '2' } })
      if (res.meta.status !== 200) {
        return this.$message.error('商品分类获取失败')
      }

      this.parentCateList = res.data
    },
    parentCateChange () {
      if (this.selectdCateId.length > 0) {
        this.addCateFrom.cat_pid = this.selectdCateId[this.selectdCateId.length - 1]
        this.addCateFrom.cat_level = this.selectdCateId.length
        return
      } else {
        this.addCateFrom.cat_pid = 0
        this.addCateFrom.cat_level = 0
      }
    },
    async addCate () {
      this.$refs.addCateFromRef.validate(async vaild => {
        console.log(this.addCateFrom);

        const { data: res } = await this.$http.post('categories', this.addCateFrom)
        if (res.meta.status !== 201) {
          return this.$message.error('商品分类添加失败')
        }

        this.getCateList()
        this.isAddDialogVisible = false
        this.$message.success('商品分类添加成功')
      })
    },
    addDialogColse () {
      this.$refs.addCateFromRef.resetFields()
      this.selectdCateId = []
      this.addCateFrom.cat_level = 0
      this.addCateFrom.cat_pid = 0
    },
    editDialog (id) {
    }
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin-top: 15px;
}
</style>
